<article id="wikiArticle">
<div></div>
<p><strong><code>toString()</code> </strong>方法返回一个表示当前函数源代码的字符串。</p>
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/function-tostring.html" width="100%"></iframe></div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code><var>function</var>.toString()</code></code></pre>
<h3 id="返回值">返回值</h3>
<p>表示函数源代码的一个字符串</p>
<h2 id="Description" name="Description">描述</h2>
<p><a href="Reference/Function" title="此页面仍未被本地化, 期待您的翻译!"><code>Function</code></a>对象覆盖了从<a href="Reference/Global_Objects/Object" title="Object 构造函数创建一个对象包装器。"><code>Object</code></a>继承来的<a href="Reference/Global_Objects/Object/toString" title="toString() 方法返回一个表示该对象的字符串。"><code>toString</code></a> 方法。对于用户定义的 <a href="Reference/Function" title="此页面仍未被本地化, 期待您的翻译!"><code>Function</code></a> 对象，<code>toString</code>方法返回一个字符串，其中包含用于定义函数的源文本段。</p>
<p>在<a href="Reference/Function" title="此页面仍未被本地化, 期待您的翻译!"><code>Function</code></a>需要转换为字符串时，通常会自动调用函数的 <code>toString </code>方法。</p>
<p>若 <code>this</code> 不是 <code>Function </code>对象，则 <code>toString()</code> 方法将抛出 <a href="Reference/Global_Objects/TypeError" title="TypeError（类型错误） 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a>  ("Function.prototype.toString called on incompatible object") 异常，比如 <a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为（如属性查找，赋值，枚举，函数调用等）。"><code>Proxy</code></a> 对象就会抛出异常。</p>
<pre><code class="language-js example-bad">Function.prototype.toString.call('foo'); // TypeError
</code></pre>
<p>如果是在内置函数或由 <code>Function.prototype.bind </code>返回的函数上调用 <code>toString()</code>，则<code>toString()</code> 返回原生代码字符串，如下</p>
<pre><code  class="language-javascript">"function () {\n    [native code]\n}"
</code></pre>
<p>若是在由 <code>Function</code> 构造器生成的函数上调用 <code>toString()</code> ，则 <code>toString()</code> 返回创建后的函数源码，包括形参和函数体，函数名为 "anonymous"。</p>
<h2 id="示例">示例</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Function</th>
<th scope="col">Function.prototype.toString result</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<pre>
function f(){}</code></pre>
</td>
<td>
<pre>
"function f(){}"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
class A { a(){} }</code></pre>
</td>
<td>
<pre>
"class A { a(){} }"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
function* g(){}</code></pre>
</td>
<td>
<pre>
"function* g(){}"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
a =&gt; a</code></pre>
</td>
<td>
<pre>
"a =&gt; a"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
({ a(){} }.a)</code></pre>
</td>
<td>
<pre>
"a(){}"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
({ *a(){} }.a)</code></pre>
</td>
<td>
<pre>
"*a(){}"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
({ [0](){} }[0])</code></pre>
</td>
<td>
<pre>
"[0](){}"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
Object.getOwnPropertyDescriptor({
    get a(){}
}, "a").get</code></pre>
</td>
<td>
<pre>
"get a(){}"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
Object.getOwnPropertyDescriptor({
    set a(x){}
}, "a").set</code></pre>
</td>
<td>
<pre>
"set a(x){}"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
Function.prototype.toString</code></pre>
</td>
<td>
<pre>
"function toString() { [native code] }"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
(function f(){}.bind(0))</code></pre>
</td>
<td>
<pre>
"function () { [native code] }"</code></pre>
</td>
</tr>
<tr>
<td>
<pre>
Function("a", "b")</code></pre>
</td>
<td>
<pre>
"function anonymous(a\n) {\nb\n}"</code></pre>
</td>
</tr>
</tbody>
</table>
<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范版本</th>
<th scope="col">规范状态</th>
<th scope="col">注解</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf" hreflang="en" lang="en" rel="noopener" title="ECMAScript 1st Edition (ECMA-262)">ECMAScript 1st Edition (ECMA-262)</a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>初始定义。在 JavaScript 1.1 中实现。</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.tostring" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Function.prototype.toString</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>对字符串表示增加了更多的特定需求。</td>
</tr>
<tr>
<td><a class="external" href="http://tc39.github.io/Function-prototype-toString-revision/" rel="noopener">Function.prototype.toString revision</a></td>
<td>Draft</td>
<td>对内置函数与行尾表示进行标准化。</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-function.prototype.tostring" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Function.prototype.toString</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><code>toString</code></th><td class="bc-supports-yes bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              12</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              1</td><td class="bc-supports-yes bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              5</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              4</td><td class="bc-supports-yes bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td></tr><tr><th scope="row">Support of <a class="external" href="http://tc39.github.io/Function-prototype-toString-revision/" rel="noopener">toString revision</a></th><td class="bc-supports-no bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              54</td><td class="bc-supports-no bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              54</td><td class="bc-supports-no bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-no bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td></tr></tbody></table><section class="bc-legend" id="sect1"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
<span>Full support</span>
                  
                </abbr></span></dt><dd>Full support</dd><dt><span class="bc-supports-no bc-supports">
<abbr class="bc-level bc-level-no only-icon" title="No support">
<span>No support</span>
                  
                </abbr></span></dt><dd>No support</dd></dl></section></div><p></p>
</div>
<h2 id="附注（针对Firefox）">附注（针对Firefox）</h2>
<ul>
<li>从Firefox 17开始，<code>Function.prototype.toString()</code> 通过保存函数源码的方式来实现，而之前是通过反编译器反编译函数字节码的方式来实现。反编译器已经被移除，因此我们不再需要 <code>indentation</code> 参数。查看 <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=761723" rel="noopener" title="FIXED: implement toString of function objects by saving source">bug 761723</a> 获得更多信息。</li>
<li>从Firefox 38开始，<code>Function.prototype.toString()</code> 会对 <a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为（如属性查找，赋值，枚举，函数调用等）。"><code>Proxy</code></a> 对象抛出异常 (<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1100936" rel="noopener" title="FIXED: Handle various operations on revoked proxies">bug 1100936</a>)。</li>
</ul>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Object/toString" title="toString() 方法返回一个表示该对象的字符串。"><code>Object.prototype.toString()</code></a></li>
</ul>
</article>